import Vue from 'vue';
import VueRouter from 'vue-router';
// import Login from '@/views/Login';
// import Home from '@/views/Home';
// import User from '@/views/User';
// import Role from '@/views/Role';
// import Menu from '@/views/Menu';
// import Permission from '@/views/Permission';
// import Department from '@/views/Department';

//登录
const Login = () => import("@/views/Login.vue");
//主页
const Home = () => import("@/views/Home.vue");
//系统
const User = () => import("@/views/User.vue");
const Role = () => import("@/views/Role.vue");
const Menu = () => import("@/views/Menu.vue");
const Permission = () => import("@/views/Permission.vue");
const Department = () => import("@/views/Department.vue");

Vue.use(VueRouter);

const router = new VueRouter({
    mode: "history",
    routes: [{
        path: '*',
        meta: {
            keepAlive: true
        },
        redirect: '/home'
    }, {
        path: '/login',
        name: 'login',
        meta: {
            keepAlive: false
        },
        components: {login: Login}
    }, {
        path: '/home',
        name: 'home',
        meta: {
            keepAlive: true
        },
        component: Home
    }, {
        path: '/user',
        name: 'user',
        meta: {
            keepAlive: true
        },
        component: User
    }, {
        path: '/role',
        name: 'role',
        meta: {
            keepAlive: true
        },
        component: Role
    }, {
        path: '/menu',
        name: 'menu',
        meta: {
            keepAlive: true
        },
        component: Menu
    }, {
        path: '/permission',
        name: 'permission',
        meta: {
            keepAlive: true
        },
        component: Permission
    }, {
        path: '/department',
        name: 'department',
        meta: {
            keepAlive: true
        },
        component: Department
    }]
});
router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        localStorage.removeItem('userInfo');
    }
    console.log("path===" + to.fullPath);
    // let menus = localStorage.getItem('menus') ? JSON.parse(localStorage.getItem('menus')) : [];
    if (!localStorage.getItem('userInfo') && to.fullPath !== '/login') {
        next('/login')
    } else if (to.fullPath === '/login') {
        next();
    }
    // else if (GetByUrl(menus, to.fullPath)) {
    //     next();
    // }
    else if (to.fullPath === '/home') {
        next();
    }
    next();
    // next(false);
});
export default router;
// const GetByUrl = function (data = [], url = '/') {
//     let path = '/' + url.split('/')[1];
//     let flag = false;
//     let map = (d, u) => {
//         d.forEach(item => {
//             if (item.url === u) {
//                 flag = true
//             } else if (item.subMenus) {
//                 map(item.subMenus, u)
//             }
//         });
//     };
//     map(data, path);
//     return flag;
// };